package com.cjhhv.visual.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.cjhhv.base.util.PageUtil;
import com.cjhhv.communal.api.FileUploadApi;
import com.cjhhv.communal.model.FileDataModel;
import com.cjhhv.visual.dao.DashboardCategoryDao;
import com.cjhhv.visual.dao.DashboardDao;
import com.cjhhv.visual.dao.DashboardThemeDao;
import com.cjhhv.visual.model.DashboardCategoryModel;
import com.cjhhv.visual.model.DashboardLogModel;
import com.cjhhv.visual.model.DashboardModel;
import com.cjhhv.visual.model.DashboardThemeModel;
import com.cjhhv.visual.service.DashboardLogService;
import com.cjhhv.visual.service.DashboardService;
import com.github.pagehelper.PageInfo;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional( rollbackFor = Exception.class)
public class DashboardServiceImpl implements DashboardService {


    private DashboardDao dashboardDao;
    private DashboardThemeDao dashboardThemeDao;

    private DashboardCategoryDao dashboardCategoryDao;

    private DashboardLogService dashboardLogService;


    private FileUploadApi fileUploadApi;


    public Map<String,Object> initDashboardAddData(String id){
        Map<String,Object> data = new HashMap<>();
        if(StrUtil.isNotEmpty(id)){
            DashboardModel dashboardData =  this.queryDashboardById(id);
            data.put("dashboardData",dashboardData);
        }

        List<DashboardThemeModel> dashboardThemeList = this.dashboardThemeDao.queryDashboardThemeList();
        data.put("dashboardThemeList",dashboardThemeList);


        List<DashboardCategoryModel> dashboardCategoryList = this.dashboardCategoryDao.queryDashboardCategoryList(new DashboardCategoryModel());
        data.put("dashboardCategoryList",dashboardCategoryList);

        return data;
    }

    public int queryDashboardIsExist(DashboardModel parameter){
        return this.dashboardDao.queryDashboardIsExist(parameter);
    }

    public Map<String,Object> initDashboardListData(){
        Map<String,Object> data = new HashMap<>();
        List<DashboardCategoryModel> dashboardCategoryList = this.dashboardCategoryDao.queryDashboardCategoryList(new DashboardCategoryModel());
        data.put("dashboardCategoryList",dashboardCategoryList);
        return data;
    }

    public PageInfo<DashboardModel> queryDashboardPageList(DashboardModel parameter){
        return PageUtil.createPageQuery(parameter,()->this.dashboardDao.queryDashboardPageList(parameter));
    }



    public DashboardModel queryDashboardById(String dashboardId){
        DashboardModel  dashboardModel =   this.dashboardDao.queryDashboardById(dashboardId);
        if(null!=dashboardModel && null!=dashboardModel.getThemeId() && !"".equals(dashboardModel.getThemeId())){
            DashboardThemeModel dashboardThemeModel = this.dashboardThemeDao.queryDashboardThemeById(dashboardModel.getThemeId());
            if(null!=dashboardThemeModel){
                dashboardModel.setDashboardTheme(dashboardThemeModel);
            }
        }

        if(null!=dashboardModel && null!=dashboardModel.getDashboardCategoryId() && !"".equals(dashboardModel.getDashboardCategoryId())){
            DashboardCategoryModel dashboardCategoryModel = this.dashboardCategoryDao.queryDashboardCategoryById(dashboardModel.getDashboardCategoryId());
            if(null!=dashboardCategoryModel){
                dashboardModel.setDashboardCategory(dashboardCategoryModel);
            }
        }

        return dashboardModel;
    }


    @GlobalTransactional
    public DashboardModel saveDashboard(DashboardModel parameter){
        if(StrUtil.isEmpty(parameter.getDashboardId())){
            parameter.setDashboardId(UUID.randomUUID().toString());
            //更新对应的文件上传数据
            if(StrUtil.isNotEmpty(parameter.getDashboardTempId())){
                FileDataModel fileDataModel = new FileDataModel();
                fileDataModel.setBusinessId(parameter.getDashboardTempId());
                fileDataModel.setUpdateUser(parameter.getCreateUser());
                fileDataModel.setUpdateUserId(parameter.getCreateUserId());
                fileDataModel.setNewBusinessId(parameter.getDashboardId());
                this.fileUploadApi.updateBusinessIdByBusinessId(fileDataModel);
            }
            int count = this.dashboardDao.saveDashboard(parameter);
            if(count>0){
                this.saveDashboardLog(parameter);
            }

            return count>0?parameter:null;
        }else{
            parameter.setUpdateUser(parameter.getCreateUser());
            parameter.setUpdateUserId(parameter.getCreateUserId());
            int count =  this.dashboardDao.updateDashboardById(parameter);
            if(count>0){
                this.saveDashboardLog(parameter);
            }
            return count>0?parameter:null;
        }
    }

    private void saveDashboardLog(DashboardModel parameter){
        DashboardLogModel dashboardLogModel = new DashboardLogModel();
        BeanUtils.copyProperties(parameter,dashboardLogModel);
        this.dashboardLogService.saveDashboardLog(dashboardLogModel);
    }


    public int updateDashboardById(DashboardModel parameter){
        return this.dashboardDao.updateDashboardById(parameter);
    }


    public int deleteDashboardByIds(DashboardModel parameter){
        return this.dashboardDao.deleteDashboardByIds(parameter);
    }


    public DashboardDao getDashboardDao() {
        return dashboardDao;
    }

    @Autowired
    public void setDashboardDao(DashboardDao dashboardDao) {
        this.dashboardDao = dashboardDao;
    }

    public DashboardThemeDao getDashboardThemeDao() {
        return dashboardThemeDao;
    }

    @Autowired
    public void setDashboardThemeDao(DashboardThemeDao dashboardThemeDao) {
        this.dashboardThemeDao = dashboardThemeDao;
    }

    public FileUploadApi getFileUploadApi() {
        return fileUploadApi;
    }


    @Autowired
    public void setFileUploadApi(FileUploadApi fileUploadApi) {
        this.fileUploadApi = fileUploadApi;
    }

    public DashboardCategoryDao getDashboardCategoryDao() {
        return dashboardCategoryDao;
    }

    @Autowired
    public void setDashboardCategoryDao(DashboardCategoryDao dashboardCategoryDao) {
        this.dashboardCategoryDao = dashboardCategoryDao;
    }

    public DashboardLogService getDashboardLogService() {
        return dashboardLogService;
    }

    @Autowired
    public void setDashboardLogService(DashboardLogService dashboardLogService) {
        this.dashboardLogService = dashboardLogService;
    }
}
